package org.itboys.core.controller.front.small;

import com.wordnik.swagger.annotations.Api;
import com.wordnik.swagger.annotations.ApiOperation;
import com.wordnik.swagger.annotations.ApiParam;
import org.itboys.commons.CommonConstants;
import org.itboys.core.dto.order.SundayOrderDto;
import org.itboys.core.service.mysql.front.SundayFrontOrderService;
import org.itboys.core.util.AccountIdHoldUtils;
import org.itboys.core.util.ResponseMessage;
import org.itboys.framework.spring.controller.BaseController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 作者：zzq
 * 时间：2018/10/23
 * 描述：订单
 */
@Controller
@RequestMapping("/sunday/small/order")
@Api(value = "order", description = "订单")
public class SundaySmallOrderController extends BaseController {
    @Autowired
    private SundayFrontOrderService orderService;

    /**
     * 创建订单
     *
     * @param request
     * @param response
     * @param memberId
     * @param cartIds
     * @param remark
     * @param addressId
     */
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    @ApiOperation(value = "生成订单")
    public void create(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(value = "用户ID") @RequestParam(value = "memberId", required = true) Long memberId,
            @ApiParam(value = "购物车ID集合(非商品ID)。如：cardIds=1,2,3") @RequestParam(value = "cartIds", required = true) List<Long> cartIds,
            @ApiParam(value = "用户备注") @RequestParam(value = "remark", required = false) String remark,
            @ApiParam(value = "收货地址ID") @RequestParam(value = "addressId", required = false) Long addressId
    ) throws IOException {
        Map<String, Object> result = orderService.createOrder(memberId, cartIds, remark, addressId);
        ResponseMessage.success(result);
    }

    /**
     * @param request
     * @param response
     * @param memberId
     * @param orderStatus
     */
    @RequestMapping(value = "/select", method = RequestMethod.POST)
    @ApiOperation(value = "查询订单列表")
    public void select(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(value = "用户ID") @RequestParam(value = "memberId", required = true) Long memberId,
            @ApiParam(value = "订单状态集合") @RequestParam(value = "orderStatus", required = false) Integer orderStatus
    ) throws IOException {
        List<SundayOrderDto> result = orderService.selectOrder(request, memberId, orderStatus);
        ResponseMessage.success(result);
    }

    /**
     * @param request
     * @param response
     * @param orderId
     */
    @RequestMapping(value = "/findOne", method = RequestMethod.POST)
    @ApiOperation(value = "查询订单详情")
    public void findOne(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(value = "订单ID") @RequestParam(value = "orderId", required = true) Long orderId
    ) throws IOException {
        SundayOrderDto result = orderService.findOne(orderId);
        ResponseMessage.success(result);
    }



    /**
     * 订单收货
     * @param request
     * @param response
     * @param orderId
     */
    @RequestMapping(value = "/receiveOrder", method = RequestMethod.POST)
    @ApiOperation(value = "订单收货")
    public void receiveOrder(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(value = "订单ID") @RequestParam(value = "orderId", required = true) Long orderId
    ) throws IOException {
        orderService.receiveOrder(orderId);
        ResponseMessage.success();
    }

    /**
     *
     * @param request
     * @param response
     * @param orderId
     */
    @RequestMapping(value = "/cancelOrder", method = RequestMethod.POST)
    @ApiOperation(value = "取消订单")
    public void cancelOrder(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(value = "订单ID") @RequestParam(value = "orderId", required = true) Long orderId
    ) {
        orderService.cancelOrder(orderId);
        ResponseMessage.success();
    }

    /**
     * 打开分享页   收取礼物
     * @param request
     * @param response
     * @param orderId
     */
    @RequestMapping(value = "/receiveGift", method = RequestMethod.POST)
    @ApiOperation(value = "收取礼物")
    public void receiveGift(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(value = "订单ID") @RequestParam(value = "orderId", required = true) Long orderId,
            @ApiParam(value = "收取人ID") @RequestParam(value = "receiveId", required = true) Long receiveId
    ) {
        Long result=orderService.receiveGift(orderId, receiveId);
        ResponseMessage.success(result);
    }

    /**
     * 打开礼物
     * @param request
     * @param response
     * @param giftId
     * @param receiveName
     * @param receiveMobile
     * @param provinceId
     * @param provinceName
     * @param cityId
     * @param cityName
     * @param districtId
     * @param districtName
     * @param receiveAddress
     * @throws IOException
     */
    @RequestMapping(value = "/openGift", method = RequestMethod.POST)
    @ApiOperation(value = "打开礼物")
    public void openGift(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(value = "礼物ID") @RequestParam(value = "giftId", required = true) Long giftId,
            @ApiParam(value = "收货人姓名") @RequestParam(value = "receiveName", required = true) String receiveName,
            @ApiParam(value = "手机号码") @RequestParam(value = "receiveMobile", required = true) String receiveMobile,
            @ApiParam(value = "省id") @RequestParam(value = "provinceId", required = true) Long provinceId,
            @ApiParam(value = "省名称") @RequestParam(value = "provinceName", required = true) String provinceName,
            @ApiParam(value = "市id") @RequestParam(value = "cityId", required = true) Long cityId,
            @ApiParam(value = "市名称") @RequestParam(value = "cityName", required = true) String cityName,
            @ApiParam(value = "区县id") @RequestParam(value = "districtId", required = true) Long districtId,
            @ApiParam(value = "区县名称") @RequestParam(value = "districtName", required = true) String districtName,
            @ApiParam(value = "详细地址") @RequestParam(value = "receiveAddress", required = true) String receiveAddress
    ) throws IOException {
        orderService.openGift(giftId, receiveName, receiveMobile,
                provinceId, provinceName, cityId, cityName, districtId, districtName, receiveAddress);
        ResponseMessage.success();
    }

    /**
     * 江南布衣申请退款
     * @param request
     * @param response
     * @param orderId
     * @throws IOException
     */
    @RequestMapping(value = "/backOrder", method = RequestMethod.POST)
    @ApiOperation(value = "订单退款")
    public void backOrder(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(value = "订单ID") @RequestParam(value = "orderId", required = true) Long orderId
    ) throws IOException {
        orderService.backOrder(orderId);
        ResponseMessage.success();
    }


    /**
     * 江南布衣申请退货
     * @param request
     * @param response
     * @param orderId
     * @throws IOException
     */
    @RequestMapping(value = "/returnOrder", method = RequestMethod.POST)
    @ApiOperation(value = "订单退货")
    public void returnOrder(
            HttpServletRequest request,
            HttpServletResponse response,
            @ApiParam(value = "订单ID") @RequestParam(value = "orderId", required = true) Long orderId
    ) throws IOException {
        orderService.returnOrder(orderId);
        ResponseMessage.success();
    }

}
